package com.jay.io.clazz;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileCls {
	public static void main(String[] args) throws IOException {
		File file = new File("target/file.txt");
		if (file.exists()) {
			System.out.println("file.txt already exists.");
			file.delete();
		}
		file.createNewFile();

		System.out.println("-----------");
		System.out.println(file.getAbsolutePath());
		System.out.println(file.getCanonicalPath());
		System.out.println(file.getFreeSpace() + "/" + file.getUsableSpace()
				+ "/" + file.getTotalSpace());
		System.out.println(file.getName());
		System.out.println(file.getParent());
		System.out.println(file.getPath());
		System.out.println(file.getAbsoluteFile());
		System.out.println(file.getCanonicalFile());
		System.out.println(file.getParentFile());
		System.out.println("-----------");

		System.out.println(file.canExecute());
		System.out.println(file.canRead());
		System.out.println(file.canWrite());
		file.setReadOnly();
		System.out.println(file.canExecute());
		System.out.println(file.canRead());
		System.out.println(file.canWrite());
		try (FileWriter fw = new FileWriter(file);) {
			fw.write("xxx");
		} catch (Exception e) {
			e.printStackTrace();
		}
		file.setWritable(true);
		FileWriter fw = new FileWriter(file);
		System.out.println(file.canExecute());
		System.out.println(file.canRead());
		System.out.println(file.canWrite());
		fw.write("xxx");
		fw.close();

		file.deleteOnExit();
		System.out.println("-----------");
		File path = new File("./target");
		if (path.isDirectory()) {
			File[] fs = path.listFiles();
			for (File f : fs) {
				System.out.println(f.getName());
			}
			System.out.println("-----------");
			File newPath = new File("./xxx");
			newPath.mkdir();
			if (newPath.exists()) {
				System.out.println("xxx is created.");
				File renPath = new File("./aaa");
				newPath.renameTo(renPath);
				System.out.println(renPath.toURI());
				newPath.delete();
				renPath.deleteOnExit();
			}
		}
	}
}
